// miniprogram/pages/bar/index/managers/belas/belas.js
const APIUrl = require('../../../../../tools/APIurl');
const mAPIUrl = new APIUrl()
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topTitle: [{
      title: "已到账",
      titleId: "1",
      creatPoint: "set_aside",
      accounts:0

    }, {
      title: "应收账",
      titleId: "2",
      creatPoint: "used",
      accounts:3
    }],
    bill:[{
      rent:'1880.00',
      tenants:'僵尸',
      roomNumber:'606',
      assertTrue:'2021-02-22',
      show:false,
      remindRent:0,
    },{
      rent:'1600.00',
      tenants:'猪头',
      roomNumber:'607',
      assertTrue:'2021-02-22',
      show:true,
      remindRent:5,
    },{
      rent:'230.00',
      tenants:'史莱姆',
      roomNumber:'202',
      assertTrue:'2021-02-22',
      show:false,
      remindRent:0,
    },
  ],
    chartSelect: 1,
    idx: "1",
    index:0,
  },
  selectApply: function (e) {
    var that = this;
    var id = e.target.dataset.id
    var list = that.data.topTitle
    that.setData({
        idx: id,
        chartSelect: id,
        currentPoint: list[id - 1].creatPoint
      }),
      console.log(that.data.currentPoint)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      that = this
      var year=new Date().getFullYear()
      var month=new Date().getMonth()+1
      var day = new Date().getDate()
      // 获取当月账单
    mAPIUrl.list('/api/mgr/bill',{
      action: 'listBill',
      landlord_id: wx.getStorageSync('userInfo').id,
      end_time:year+'-'+(month < 10 ? '0' + month : month)
    },function(res){
      console.log(res)
      var bill = []
      var total = 0
      var topTitle = that.data.topTitle
      topTitle[1].accounts=res.length
      for(var i =0;i<res.length;i++){
        var obj = {
          id:res[i].id,
          rent:res[i].all_cost,
          tenants:res[i].name,
          roomNumber:res[i].room__Room_number,
          assertTrue:res[i].end_time,
          show:false,
          remindRent:0,
        }
        total += parseFloat(res[i].all_cost)
        bill.push(obj)
      }
      that.setData({
        bill:bill,
        total:total,
        topTitle:topTitle
      })
    })
    // 获取当月已收到账单
    mAPIUrl.list('/api/mgr/bill',{
      action: 'listReceivedBill',
      landlord_id: wx.getStorageSync('userInfo').id,
      end_time:year+'-'+(month < 10 ? '0' + month : month)
    },function(res){
      console.log(res)
      var receivedBill = []
      var receivedTotal = 0
      var topTitle = that.data.topTitle
      topTitle[0].accounts=res.length
      for(var i =0;i<res.length;i++){
        var obj = {
          id:res[i].id,
          rent:res[i].all_cost,
          tenants:res[i].name,
          roomNumber:res[i].room__Room_number,
          assertTrue:res[i].end_time,
          show:false,
          remindRent:0,
        }
        receivedTotal += parseFloat(res[i].all_cost)
        receivedBill.push(obj)
      }
      that.setData({
        receivedBill:receivedBill,
        receivedTotal:receivedTotal,
        topTitle:topTitle
      })
    })
      // 获取地址信息
      mAPIUrl.list('/api/mgr/address',{
        action: 'list',
        user_id: wx.getStorageSync('userInfo').id
      },function(res){
        console.log(res)
        var array = []
        var arrayIndex=[]
        for(var i = 0;i<res.length;i++){
          array=array.concat(res[i].community+res[i].number)
          arrayIndex=arrayIndex.concat(res[i].id)
        }
        that.setData({
          array:array,
          arrayIndex:arrayIndex
        })
      })
  },
  doChange(e){
    console.log(e),
    mAPIUrl.list('/api/mgr/bill',{
      action: 'listBill',
      landlord_id: wx.getStorageSync('userInfo').id,
      end_time:e.detail.date
    },function(res){
      console.log(res)
      var bill = []
      var total = 0
      var topTitle = that.data.topTitle
      topTitle[1].accounts=res.length
      for(var i =0;i<res.length;i++){
        var obj = {
          id:res[i].id,
          rent:res[i].all_cost,
          tenants:res[i].name,
          roomNumber:res[i].room__Room_number,
          assertTrue:res[i].end_time,
          show:false,
          remindRent:0,
        }
        total += parseFloat(res[i].all_cost)
        bill.push(obj)
      }
      that.setData({
        bill:bill,
        total:total,
        topTitle:topTitle
      })
    })
    // 获取当月已收到账单
    mAPIUrl.list('/api/mgr/bill',{
      action: 'listReceivedBill',
      landlord_id: wx.getStorageSync('userInfo').id,
      end_time:e.detail.date
    },function(res){
      console.log(res)
      var receivedBill = []
      var receivedTotal = 0
      var topTitle = that.data.topTitle
      topTitle[0].accounts=res.length
      for(var i =0;i<res.length;i++){
        var obj = {
          id:res[i].id,
          rent:res[i].all_cost,
          tenants:res[i].name,
          roomNumber:res[i].room__Room_number,
          assertTrue:res[i].end_time,
          show:false,
          remindRent:0,
        }
        receivedTotal += parseFloat(res[i].all_cost)
        receivedBill.push(obj)
      }
      that.setData({
        receivedBill:receivedBill,
        receivedTotal:receivedTotal,
        topTitle:topTitle
      })
    })
  },
  rentBtn(e){
    wx.showToast({
      title: '已催租',
    })
  },
  receivedBtn(e){
    console.log(e)
    mAPIUrl.update('/api/mgr/bill',{
      action: 'modify',
      id:e.currentTarget.dataset.id,
      newdata:{
        type:1,
      }
    },function(res){
     
    })
    that.onShow()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    that = this
      var year=new Date().getFullYear()
      var month=new Date().getMonth()+1
      var day = new Date().getDate()
      // 获取当月账单
    mAPIUrl.list('/api/mgr/bill',{
      action: 'listBill',
      landlord_id: wx.getStorageSync('userInfo').id,
      end_time:year+'-'+(month < 10 ? '0' + month : month)
    },function(res){
      console.log(res)
      var bill = []
      var total = 0
      var topTitle = that.data.topTitle
      topTitle[1].accounts=res.length
      for(var i =0;i<res.length;i++){
        var obj = {
          id:res[i].id,
          rent:res[i].all_cost,
          tenants:res[i].name,
          roomNumber:res[i].room__Room_number,
          assertTrue:res[i].end_time,
          show:false,
          remindRent:0,
        }
        total += parseFloat(res[i].all_cost)
        bill.push(obj)
      }
      that.setData({
        bill:bill,
        total:total,
        topTitle:topTitle
      })
    })
    // 获取当月已收到账单
    mAPIUrl.list('/api/mgr/bill',{
      action: 'listReceivedBill',
      landlord_id: wx.getStorageSync('userInfo').id,
      end_time:year+'-'+(month < 10 ? '0' + month : month)
    },function(res){
      console.log(res)
      var receivedBill = []
      var receivedTotal = 0
      var topTitle = that.data.topTitle
      topTitle[0].accounts=res.length
      for(var i =0;i<res.length;i++){
        var obj = {
          id:res[i].id,
          rent:res[i].all_cost,
          tenants:res[i].name,
          roomNumber:res[i].room__Room_number,
          assertTrue:res[i].end_time,
          show:false,
          remindRent:0,
        }
        receivedTotal += parseFloat(res[i].all_cost)
        receivedBill.push(obj)
      }
      that.setData({
        receivedBill:receivedBill,
        receivedTotal:receivedTotal,
        topTitle:topTitle
      })
    })
      // 获取地址信息
      mAPIUrl.list('/api/mgr/address',{
        action: 'list',
        user_id: wx.getStorageSync('userInfo').id
      },function(res){
        console.log(res)
        var array = []
        var arrayIndex=[]
        for(var i = 0;i<res.length;i++){
          array=array.concat(res[i].community+res[i].number)
          arrayIndex=arrayIndex.concat(res[i].id)
        }
        that.setData({
          array:array,
          arrayIndex:arrayIndex
        })
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})